package com.common.baselibrary.base;

import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import com.common.baselibrary.progressutil.KProgressHUD;
import com.finddreams.languagelib.MultiLanguageUtil;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Locale;

import butterknife.ButterKnife;
import butterknife.Unbinder;


public abstract class BaseActivity extends AppCompatActivity {
    private KProgressHUD hud;
    private Unbinder mUnbinder;
    protected int mWidth;
    protected int mHeight122;
    protected float mDensity;
    protected int mDensityDpi;

    //多语言配置初始化========
//    @Override
//    protected void attachBaseContext(Context newBase) {
//        super.attachBaseContext(MultiLanguageUtil.attachBaseContext(newBase));
//        //app杀进程启动后会调用Activity attachBaseContext
//        MultiLanguageUtil.getInstance().setConfiguration(newBase);
//    }
//
//    @Override
//    public void onConfigurationChanged(Configuration newConfig) {
//        super.onConfigurationChanged(newConfig);
//    }

    //多语言配置结束

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getContentViewLayoutID() != 0) {
            Log.e("hhhh===", "111111111111");
            setContentView(getContentViewLayoutID());
            ButterKnife.bind(this);
        } else {
            throw new IllegalArgumentException("You must return a right contentView layout resource Id");
        }
//        initReceiver();
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        mDensity = dm.density;
        mDensityDpi = dm.densityDpi;
        mWidth = dm.widthPixels;
        mHeight122 = dm.heightPixels;
        set();
        initProgress();
        beforeSetView();
    }


    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
    }

    protected abstract int getContentViewLayoutID();

    protected void beforeSetView() {

    }

    protected void toast(String s) {
        Toast.makeText(this, s, Toast.LENGTH_SHORT).show();
    }

    protected void toastLong(String s) {
        Toast.makeText(this, s, Toast.LENGTH_LONG).show();
    }

    @Override
    protected void onDestroy() {
//        unregisterReceiver(mReceiver);
        super.onDestroy();
        closeProgress();
        if (mUnbinder != null && mUnbinder != Unbinder.EMPTY)
            mUnbinder.unbind();
    }

    private void initProgress() {
        hud = KProgressHUD.create(this);
    }

    public void showProgress() {
        if (hud == null) {
            hud = KProgressHUD.create(this);
        }
        if (!hud.isShowing()) {
            hud.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setDimAmount(0.5f)
                    .setLabel("正在加载...")
                    .show();
        }
    }

    public void showProgress(String content) {
        if (hud == null) {
            hud = KProgressHUD.create(this);
        }
        if (!hud.isShowing()) {
            hud.setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                    .setDimAmount(0.5f)
                    .setLabel(content)
                    .show();
        }
    }

    public void closeProgress() {
        if (hud != null && hud.isShowing()) {
            hud.dismiss();
        }
    }

    /**
     * app 统计
     */
    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

//    private NetworkReceiver mReceiver;
//
//    private void initReceiver() {
//        mReceiver = new NetworkReceiver();
//        IntentFilter filter = new IntentFilter();
//        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
//        registerReceiver(mReceiver, filter);
//    }
//
//    //监听网络状态的广播
//    private class NetworkReceiver extends BroadcastReceiver {
//        @Override
//        public void onReceive(Context context, Intent intent) {
//            if (intent != null && intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
//                ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//                NetworkInfo activeInfo = manager.getActiveNetworkInfo();
//                if (null == activeInfo) {
//                    isNetWork = false;
//                    netOnListenerError();
//                } else {
//                    isNetWork = true;
//                    netOnListenerSuccess();
//                }
//            }
//        }
//    }

//    private boolean isNetWork = true;

    /**
     * 获取到网络连接的数据
     */
//    public boolean getNetWork() {
//        return isNetWork;
//    }

//    public void netOnListenerSuccess(){}

//    public void netOnListenerError(){
//
//    }
    private void set() {

    }

}
